using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using CatalogSystemProxy;
using System.Xml;

namespace CatalogManager.ProductForms
{
	/// <summary>
	/// Summary description for ProductPicker.
	/// </summary>
	public class ProductPicker : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cboCatalogs;
		private System.Windows.Forms.Label lblCatalog;
		private System.Windows.Forms.TabControl tabControlProductPicker;
		private System.Windows.Forms.TabPage tabPageCatalog;
		private System.Windows.Forms.TabPage tabPageKeyWord;
		private System.Windows.Forms.TabPage tabPageAdvanceSearch;
		private System.Windows.Forms.Label lblSelection;
		public System.Windows.Forms.TextBox txtSelection;
		private System.Windows.Forms.Button btnOk;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.TextBox txtKeyWord;
		private System.Windows.Forms.Button btnFindNow;
		private System.Windows.Forms.Label lblKeyWords;
		private System.Windows.Forms.ListView listView3;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button btnAdvFindNow;
		private System.Windows.Forms.TreeView treeViewCategories;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ListView Datalist;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ListView listViewKeyWord;
		private CatalogSystem CSService = null;
		private System.Windows.Forms.ImageList TreeViewimageList;
		public bool isCancel=false;

		public ProductPicker()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ProductPicker));
			this.tabControlProductPicker = new System.Windows.Forms.TabControl();
			this.tabPageCatalog = new System.Windows.Forms.TabPage();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.Datalist = new System.Windows.Forms.ListView();
			this.TreeViewimageList = new System.Windows.Forms.ImageList(this.components);
			this.treeViewCategories = new System.Windows.Forms.TreeView();
			this.tabPageKeyWord = new System.Windows.Forms.TabPage();
			this.lblKeyWords = new System.Windows.Forms.Label();
			this.listViewKeyWord = new System.Windows.Forms.ListView();
			this.btnFindNow = new System.Windows.Forms.Button();
			this.txtKeyWord = new System.Windows.Forms.TextBox();
			this.tabPageAdvanceSearch = new System.Windows.Forms.TabPage();
			this.btnAdvFindNow = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.listView3 = new System.Windows.Forms.ListView();
			this.cboCatalogs = new System.Windows.Forms.ComboBox();
			this.lblCatalog = new System.Windows.Forms.Label();
			this.lblSelection = new System.Windows.Forms.Label();
			this.txtSelection = new System.Windows.Forms.TextBox();
			this.btnOk = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.tabControlProductPicker.SuspendLayout();
			this.tabPageCatalog.SuspendLayout();
			this.tabPageKeyWord.SuspendLayout();
			this.tabPageAdvanceSearch.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControlProductPicker
			// 
			this.tabControlProductPicker.Controls.AddRange(new System.Windows.Forms.Control[] {
																								  this.tabPageCatalog,
																								  this.tabPageKeyWord,
																								  this.tabPageAdvanceSearch});
			this.tabControlProductPicker.Location = new System.Drawing.Point(8, 48);
			this.tabControlProductPicker.Name = "tabControlProductPicker";
			this.tabControlProductPicker.SelectedIndex = 0;
			this.tabControlProductPicker.Size = new System.Drawing.Size(440, 256);
			this.tabControlProductPicker.TabIndex = 0;
			// 
			// tabPageCatalog
			// 
			this.tabPageCatalog.Controls.AddRange(new System.Windows.Forms.Control[] {
																						 this.splitter1,
																						 this.Datalist,
																						 this.treeViewCategories});
			this.tabPageCatalog.Location = new System.Drawing.Point(4, 22);
			this.tabPageCatalog.Name = "tabPageCatalog";
			this.tabPageCatalog.Size = new System.Drawing.Size(432, 230);
			this.tabPageCatalog.TabIndex = 0;
			this.tabPageCatalog.Text = "Catalog";
			// 
			// splitter1
			// 
			this.splitter1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.splitter1.Location = new System.Drawing.Point(184, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 230);
			this.splitter1.TabIndex = 2;
			this.splitter1.TabStop = false;
			// 
			// Datalist
			// 
			this.Datalist.Dock = System.Windows.Forms.DockStyle.Fill;
			this.Datalist.FullRowSelect = true;
			this.Datalist.LargeImageList = this.TreeViewimageList;
			this.Datalist.Location = new System.Drawing.Point(184, 0);
			this.Datalist.MultiSelect = false;
			this.Datalist.Name = "Datalist";
			this.Datalist.Size = new System.Drawing.Size(248, 230);
			this.Datalist.SmallImageList = this.TreeViewimageList;
			this.Datalist.StateImageList = this.TreeViewimageList;
			this.Datalist.TabIndex = 1;
			this.Datalist.Click += new System.EventHandler(this.Datalist_Click);
			// 
			// TreeViewimageList
			// 
			this.TreeViewimageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
			this.TreeViewimageList.ImageSize = new System.Drawing.Size(16, 16);
			this.TreeViewimageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("TreeViewimageList.ImageStream")));
			this.TreeViewimageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// treeViewCategories
			// 
			this.treeViewCategories.Dock = System.Windows.Forms.DockStyle.Left;
			this.treeViewCategories.ImageIndex = 3;
			this.treeViewCategories.ImageList = this.TreeViewimageList;
			this.treeViewCategories.Name = "treeViewCategories";
			this.treeViewCategories.SelectedImageIndex = 3;
			this.treeViewCategories.Size = new System.Drawing.Size(184, 230);
			this.treeViewCategories.TabIndex = 0;
			this.treeViewCategories.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeViewCategories_MouseUp);
			// 
			// tabPageKeyWord
			// 
			this.tabPageKeyWord.Controls.AddRange(new System.Windows.Forms.Control[] {
																						 this.lblKeyWords,
																						 this.listViewKeyWord,
																						 this.btnFindNow,
																						 this.txtKeyWord});
			this.tabPageKeyWord.Location = new System.Drawing.Point(4, 22);
			this.tabPageKeyWord.Name = "tabPageKeyWord";
			this.tabPageKeyWord.Size = new System.Drawing.Size(432, 230);
			this.tabPageKeyWord.TabIndex = 1;
			this.tabPageKeyWord.Text = "KeyWordSearch";
			// 
			// lblKeyWords
			// 
			this.lblKeyWords.Location = new System.Drawing.Point(16, 8);
			this.lblKeyWords.Name = "lblKeyWords";
			this.lblKeyWords.Size = new System.Drawing.Size(408, 23);
			this.lblKeyWords.TabIndex = 3;
			this.lblKeyWords.Text = "Key Words:";
			// 
			// listViewKeyWord
			// 
			this.listViewKeyWord.Location = new System.Drawing.Point(8, 72);
			this.listViewKeyWord.Name = "listViewKeyWord";
			this.listViewKeyWord.Size = new System.Drawing.Size(416, 152);
			this.listViewKeyWord.TabIndex = 2;
			this.listViewKeyWord.Click += new System.EventHandler(this.listViewKeyWord_Click);
			// 
			// btnFindNow
			// 
			this.btnFindNow.Location = new System.Drawing.Point(344, 40);
			this.btnFindNow.Name = "btnFindNow";
			this.btnFindNow.TabIndex = 1;
			this.btnFindNow.Text = "Find Now";
			this.btnFindNow.Click += new System.EventHandler(this.btnFindNow_Click);
			// 
			// txtKeyWord
			// 
			this.txtKeyWord.Location = new System.Drawing.Point(8, 40);
			this.txtKeyWord.Name = "txtKeyWord";
			this.txtKeyWord.Size = new System.Drawing.Size(328, 20);
			this.txtKeyWord.TabIndex = 0;
			this.txtKeyWord.Text = "";
			// 
			// tabPageAdvanceSearch
			// 
			this.tabPageAdvanceSearch.Controls.AddRange(new System.Windows.Forms.Control[] {
																							   this.btnAdvFindNow,
																							   this.panel1,
																							   this.listView3});
			this.tabPageAdvanceSearch.Location = new System.Drawing.Point(4, 22);
			this.tabPageAdvanceSearch.Name = "tabPageAdvanceSearch";
			this.tabPageAdvanceSearch.Size = new System.Drawing.Size(432, 230);
			this.tabPageAdvanceSearch.TabIndex = 2;
			this.tabPageAdvanceSearch.Text = "Advance Search";
			// 
			// btnAdvFindNow
			// 
			this.btnAdvFindNow.Location = new System.Drawing.Point(352, 16);
			this.btnAdvFindNow.Name = "btnAdvFindNow";
			this.btnAdvFindNow.TabIndex = 2;
			this.btnAdvFindNow.Text = "Find Now";
			// 
			// panel1
			// 
			this.panel1.Location = new System.Drawing.Point(8, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(336, 100);
			this.panel1.TabIndex = 1;
			// 
			// listView3
			// 
			this.listView3.Location = new System.Drawing.Point(8, 128);
			this.listView3.Name = "listView3";
			this.listView3.Size = new System.Drawing.Size(416, 97);
			this.listView3.TabIndex = 0;
			// 
			// cboCatalogs
			// 
			this.cboCatalogs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboCatalogs.Location = new System.Drawing.Point(136, 16);
			this.cboCatalogs.Name = "cboCatalogs";
			this.cboCatalogs.Size = new System.Drawing.Size(312, 21);
			this.cboCatalogs.TabIndex = 1;
			this.cboCatalogs.SelectedIndexChanged += new System.EventHandler(this.cboCatalogs_SelectedIndexChanged);
			// 
			// lblCatalog
			// 
			this.lblCatalog.Location = new System.Drawing.Point(16, 16);
			this.lblCatalog.Name = "lblCatalog";
			this.lblCatalog.Size = new System.Drawing.Size(112, 16);
			this.lblCatalog.TabIndex = 2;
			this.lblCatalog.Text = "Catalog:";
			// 
			// lblSelection
			// 
			this.lblSelection.Location = new System.Drawing.Point(8, 312);
			this.lblSelection.Name = "lblSelection";
			this.lblSelection.Size = new System.Drawing.Size(120, 16);
			this.lblSelection.TabIndex = 3;
			this.lblSelection.Text = "Selection:";
			// 
			// txtSelection
			// 
			this.txtSelection.Enabled = false;
			this.txtSelection.Location = new System.Drawing.Point(136, 312);
			this.txtSelection.Name = "txtSelection";
			this.txtSelection.Size = new System.Drawing.Size(304, 20);
			this.txtSelection.TabIndex = 4;
			this.txtSelection.Text = "";
			// 
			// btnOk
			// 
			this.btnOk.Location = new System.Drawing.Point(152, 344);
			this.btnOk.Name = "btnOk";
			this.btnOk.TabIndex = 5;
			this.btnOk.Text = "Ok";
			this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(248, 344);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 6;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// ProductPicker
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(456, 374);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnOk,
																		  this.txtSelection,
																		  this.lblSelection,
																		  this.lblCatalog,
																		  this.cboCatalogs,
																		  this.tabControlProductPicker});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "ProductPicker";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Product Picker";
			this.Load += new System.EventHandler(this.ProductPicker_Load);
			this.tabControlProductPicker.ResumeLayout(false);
			this.tabPageCatalog.ResumeLayout(false);
			this.tabPageKeyWord.ResumeLayout(false);
			this.tabPageAdvanceSearch.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			// exit
			isCancel=true;
			this.Close();
		}

		private void btnOk_Click(object sender, System.EventArgs e)
		{
			// save changes and close
			this.Close();
		}

		private void ProductPicker_Load(object sender, System.EventArgs e)
		{
			// read the xml file and get the file path
			XmlDocument doc = new XmlDocument();
			doc.Load("SiteConfig.xml");
			CSService = new CatalogSystem(doc.FirstChild.InnerText);
			object recInQuery = null;
			// load the catalogs
			DataSet ds = CSService.GetCatalogs(null, null,ref recInQuery);
			foreach(DataRow dr in ds.Tables[0].Rows)
			{
				cboCatalogs.Items.Add(dr["CatalogName"].ToString());
			}
			cboCatalogs.SelectedIndex=0;
		}

		private void cboCatalogs_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// get the root categories
			// clear the nodes first
			treeViewCategories.Nodes.Clear();
			// load the catalog tree
			DataSet ds = CSService.GetRootCategories(cboCatalogs.SelectedItem.ToString());
			LoadCategories( ds);

		}

		private void LoadCategories(DataSet dsCategories)
		{
			foreach(DataRow dr in dsCategories.Tables[0].Rows)
			{
				CategoryNode  cnode = new CategoryNode(dr);
				treeViewCategories.Nodes.Add(cnode);
			}
		}

		private void LoadCategories(CategoryNode categoryNode, DataSet dsCategories)
		{
			if(dsCategories!=null)
			{
				foreach(DataRow dr in dsCategories.Tables[0].Rows)
				{
					// ad the data row to each node so we don't have to 
					// go to the server again
					CategoryNode  cnode = new CategoryNode(dr);
					categoryNode.Nodes.Add(cnode);
				}
			}
		}

		private void DisplayCategoryProductProperties(DataRow dr, DataSet dsProducts)
		{
			ClearDatalist();
			Datalist.AllowColumnReorder = true;
			Datalist.View = View.Details;

			// get the properties we are supposed to show
			DataSet ds = CSService.PropertiesToDisplayInList(dr["CatalogName"].ToString());

			// Create columns
			foreach(DataRow drDisp in ds.Tables[0].Rows)
			{
				Datalist.Columns.Add(drDisp["DisplayInProductsList"].ToString(), 100, HorizontalAlignment.Left);
			}
	
			string[] lst = new string[Datalist.Columns.Count];
			
			if(dsProducts!=null)
			{
				foreach(DataRow drList in dsProducts.Tables[0].Rows)
				{
					// now iterate and get the columns
					for(int i=0;Datalist.Columns.Count>i;i++)
					{
						lst[i] = drList[Datalist.Columns[i].Text].ToString();
					}

					// create our own ProductListView so we can add more info to it
					ProductListView litem = new ProductListView(lst);
					litem.CatalogName = drList["CatalogName"].ToString();
					litem.ProductID = drList["ProductID"].ToString();
					litem.drProduct = drList;
					litem.StateImageIndex = 4;
					Datalist.Items.Add(litem);
				}
			}
		}

		private void ClearDatalist()
		{
			Datalist.Items.Clear();
			Datalist.Columns.Clear();
		}

		private void Datalist_Click(object sender, System.EventArgs e)
		{
			// get the selected item
			txtSelection.Text = ((ProductListView)Datalist.SelectedItems[0]).ProductID;
			// for right now just put the whole thing there we can clean it up later
			txtSelection.Tag = ((ProductListView)Datalist.SelectedItems[0]).drProduct;
		}

		private void btnFindNow_Click(object sender, System.EventArgs e)
		{
			// do a keyword search
			// TODO:
			// this is the same logic as used in other place need to clean it up later

			// clear the datalist and set details
			listViewKeyWord.Items.Clear();
			listViewKeyWord.Columns.Clear();
			listViewKeyWord.AllowColumnReorder = true;
			listViewKeyWord.View = View.Details;

			// get the properties we are supposed to show
			DataSet ds = CSService.PropertiesToDisplayInList(cboCatalogs.Text);

			// Create columns
			string propertiesToReturn=null;
			foreach(DataRow drDisp in ds.Tables[0].Rows)
			{
				listViewKeyWord.Columns.Add(drDisp["DisplayInProductsList"].ToString(), 100, HorizontalAlignment.Left);
				// build properties to return
				propertiesToReturn+=drDisp["DisplayInProductsList"].ToString()+",";
			}
	
			// build the search
			propertiesToReturn=propertiesToReturn.Remove(propertiesToReturn.Length-1,1);
			//string productID = CatalogTree.Nodes;
			// search
			object lTotalRecordsInQuery=null;
			// we need to get the identifaying properties
			string ProductID=null;
			string VariantID=null;

			CSService.GetIdentifyingProperties(ref ProductID, ref VariantID, cboCatalogs.Text);

			ds = CSService.Search(null, txtKeyWord.Text , cboCatalogs.Text, "28", propertiesToReturn, ProductID, false, "1", "100", null, ref lTotalRecordsInQuery);

			// populate the data list
			string[] lst = new string[Datalist.Columns.Count];
			
			foreach(DataRow drList in ds.Tables[0].Rows)
			{
				// now iterate and get the columns
				for(int i=0;listViewKeyWord.Columns.Count>i;i++)
				{
					lst[i] = drList[Datalist.Columns[i].Text].ToString();
				}

				ProductListView litem = new ProductListView(lst);
				litem.CatalogName = cboCatalogs.Text;
				litem.ProductID = drList[ProductID].ToString();
				listViewKeyWord.Items.Add(litem);
			}
		}

		private void listViewKeyWord_Click(object sender, System.EventArgs e)
		{
			// get the item for deisplay
			txtSelection.Text = ((ProductListView)listViewKeyWord.SelectedItems[0]).ProductID;
			// for right now lets just put everything there and clean it up later
			listViewKeyWord.Tag = listViewKeyWord.SelectedItems[0];
		}

		private void treeViewCategories_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			// set the selected treenode
			Point p = new Point(e.X, e.Y);
			TreeNode tnode = treeViewCategories.GetNodeAt(p);
			if(tnode!=null)
			{
				DataSet dsProducts=null;
				object lStartingRecord=0; 
				object lNumRecords=20;
				object recInQuery=null;

				CategoryNode catnode = (CategoryNode)tnode;
				// check to see if need to make a call to the server					
				if(catnode.CategoryProducts==null)
				{
					dsProducts = CSService.RootProducts(catnode.CategoryProperty["CatalogName"].ToString(), tnode.Text, lStartingRecord, lNumRecords, ref recInQuery);
					catnode.CategoryProducts = dsProducts;
				}
				else
				{
					dsProducts = catnode.CategoryProducts;
				}
				DisplayCategoryProductProperties(catnode.CategoryProperty, dsProducts);
				// check to see if we have child nodes if not then get categories
				CategoryNode nextCatNode = (CategoryNode)catnode.FirstNode;
				if(nextCatNode==null)
				{
					LoadCategories(catnode, CSService.ChildCategories(catnode.CategoryProperty["CatalogName"].ToString(), catnode.Text));
				}
				// get the selected item
				txtSelection.Text = tnode.Text;
				// for right now put the whole thing in there we can clean it up later
				txtSelection.Tag = catnode.CategoryProperty;
				// get the type of node we are dealing with
				//treeViewCategories.SelectedNode = tnode;
			}
		}
	}
}
